package org.ftz;
import com.github.junrar.Archive;
import com.github.junrar.rarfile.FileHeader;
import org.junit.Test;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipInputStream;
public class test4 {
    public List<String> getZipFileList(String zipFileName) throws IOException { //获取zip压缩包下的所有txt目录
        String input="",output="";
        ZipInputStream zipInputStream = null;
        try (FileInputStream inputStream = new FileInputStream(zipFileName)) {
            zipInputStream = new ZipInputStream(
                    new BufferedInputStream(inputStream), StandardCharsets.UTF_8);
            java.util.zip.ZipEntry ze;
            List<String> list = new ArrayList<>();
            while ((ze = zipInputStream.getNextEntry()) != null) { //循环遍历
                if (!ze.isDirectory()) {
                    String content = getZipOneTestFileContent(zipFileName, ze.getName());
                    if(ze.getName().startsWith("input")){
                        input+=content+"@";
                    }else if(ze.getName().startsWith("output")){
                        output+=content+"@";
                    }
                }
            }
            list.add(input);
            list.add(output);
            return list;
        } catch (Exception e) {
            return null;
        } finally {
            if (zipInputStream != null) {
                zipInputStream.closeEntry();
            }
        }
    }
    public String getZipOneTestFileContent(String zipFileName, String fileName) throws IOException {
        ZipInputStream zipInputStream = null;
        FileInputStream inputStream = null;
        try {
            inputStream = new FileInputStream(zipFileName);
            zipInputStream = new ZipInputStream(
                    new BufferedInputStream(inputStream), StandardCharsets.UTF_8);
            java.util.zip.ZipEntry ze;
            StringBuilder txtStr = new StringBuilder();
            while ((ze = zipInputStream.getNextEntry()) != null) {  //循环遍历
                if (!ze.isDirectory() && ze.getName().equals(fileName)) {
                    byte[] b = new byte[1024];
                    int leng;
                    while ((leng = zipInputStream.read(b)) != -1) {
                        txtStr.append(new String(b, 0, leng));
                    }
                    break;
                }
            }
            return txtStr.toString();
        } catch (Exception e) {
            return null;
        } finally {
            if (inputStream != null) {
                inputStream.close();
            }
            if (zipInputStream != null) {
                zipInputStream.close();
            }
        }
    }
    @Test
    public void test01() throws IOException {
        String path="D:\\Google\\test\\波动数列.zip";
        List<String> rarList = getZipFileList(path);
        for(String s:rarList){
            System.out.println(s);
        }
    }
}
